<template>
  <el-table
    ref="multipleTableRef"
    class="tableData table_box" :height="tabH" :style="{ width: '100%', height: tabH }"
    :data="tableData"
    stripe
    @selection-change="handleSelectionChange"
    :header-cell-style="{ background: '#F9F9F9', color: '#33302D' }"
  >
    <el-table-column
      type="selection"
      width="50"
      align="left"
    />
    <el-table-column
      label="员工姓名"
      width="160px"
      align="left"
    >
      <template #default="scope">
        <div class="cusInfo">
          <div class="name">
            {{ scope.row.work_user_name ? scope.row.work_user_name : "--" }}
          </div>
          <div class="sex">
            <img
              src="@/assets/customer/unknown.png"
              alt=""
              v-if="scope.row.work_user_gender === 0"
            />
            <img
              src="@/assets/customer/man.png"
              alt=""
              v-if="scope.row.work_user_gender === 1"
            />
            <img
              src="@/assets/customer/girl.png"
              alt=""
              v-if="scope.row.work_user_gender === 2"
            />
          </div>
        </div>
      </template>
    </el-table-column>
    <el-table-column
      label="所属部门"
      width="200px"
      align="left"
    >
      <template #default="scope">
        <div v-if="scope.row.department">
          <span
            v-for="(e,index) in scope.row.department"
            :key="e.id"
          >{{ e.name }}<span v-show="scope.row.department.length - 1 != index">、</span></span>
        </div>
        <span v-show="!scope.row.department">--</span>
      </template>
    </el-table-column>
    <el-table-column
      label="客户信息"
      width="240px"
      align="left"
    >
      <template #default="scope">
        <div class="cusInfo">
          <el-image
            :src="scope.row.cust_avatar"
            fit="cover"
            class="avatar"
          >
            <template #error>
              <div class="image-slot">
                <el-icon>
                  <icon-picture />
                </el-icon>
              </div>
            </template>
          </el-image>
          <div class="name">{{ scope.row.cust_name }}</div>
          <div class="channel">@微信</div>
          <!-- <div>@探马</div> -->
          <div class="sex">
            <img
              src="@/assets/customer/unknown.png"
              alt=""
              v-if="scope.row.cust_sex === 0"
            />
            <img
              src="@/assets/customer/man.png"
              alt=""
              v-if="scope.row.cust_sex === 1"
            />
            <img
              src="@/assets/customer/girl.png"
              alt=""
              v-if="scope.row.cust_sex === 2"
            />
          </div>
        </div>
      </template>
    </el-table-column>
    <el-table-column
      property="cust_mobile"
      label="客户手机号"
      width="170"
      align="left"
    >
      <template #default="scope">
        <span v-if="scope.row.cust_mobile">{{scope.row.cust_mobile}}</span>
        <span v-else>--</span>
      </template>
    </el-table-column>
    <el-table-column
      label="可联系员工"
      width="200"
      align="left"
    >
      <template #default="scope">
        <div>
          <el-popover
            v-if="scope.row.follows.length > 0"
            placement="top"
            :width="400"
            trigger="hover"
            popper-class="popper"
          >
            <template #reference>
              <div class="tag_box">
                <span
                  v-for="(item, index) in scope.row.follows"
                  :key="item.id"
                  size="small"
                  class="form-tags"
                >{{ item.name }}
                  <span v-show="scope.row.follows.length - 1 != index">、</span>
                </span>
              </div>
            </template>
            <div class="pop_dialog">
              <span
                v-for="(item, index) in scope.row.follows"
                :key="item.id"
                size="small"
                class="form-tags"
              >{{ item.name }}
                <span v-show="scope.row.follows.length - 1 != index">、</span>
              </span>
            </div>
          </el-popover>
          <div v-else>--</div>
        </div>
      </template>
    </el-table-column>
    <el-table-column
      label="原因"
      width="170"
      align="left"
    >
      <template #default="scope">
        <div v-if="scope.row.type === 1">员工删除</div>
        <div v-if="scope.row.type === 2">客户主动删除</div>
      </template>
    </el-table-column>
    <el-table-column
      label="删人时间"
      min-width="200"
      align="left"
    >
      <template #default="scope">
        <div>{{ scope.row.delete_time ? scope.row.delete_time : "--" }}</div>
      </template>
    </el-table-column>
    <el-table-column
      label="操作"
      align="left"
      width="300"
      fixed="right"
    >
      <template #default="scope">
        <div class="handle">
          <el-button
            size="small"
            @click="allot(scope.row)"
            v-auth="'ALLOT_STA'"
          >{{scope.row.follows.length ? "重新分配" : "分配员工"}}</el-button>
          <el-button
            size="small"
            @click="detail(scope.row)"
            v-auth="'CUST_DETIL_STA'"
          >客户详情</el-button>
          <el-button
            size="small"
            @click="remind(scope.row)"
          >提醒员工</el-button>
          <!-- <el-popconfirm :hide-icon="true"  title="提醒员工" confirm-button-text="确定发送" cancel-button-text="取消">
            <template #reference>
             
            </template>
          </el-popconfirm> -->
        </div>
      </template>
    </el-table-column>
    <template #empty>
      <div
        style="height: 500px"
        class="emty"
      >
        <img
          class="emty_img"
          src="@/assets/noData.png"
          alt=""
        />
        <div class="emty_title">{{ tableData.length || "暂无数据" }}</div>
      </div>
    </template>
  </el-table>
  <!-- 一键提醒弹框 -->
  <el-dialog
    v-model="dialogVisible"
    title="一键提醒"
    width="30%"
    :before-close="handleClose"
  >
    <span style="line-height: 20px">
      <!-- 员工 “{{ remindInfo.name }}” 删除客户 “{{ remindInfo.follow_staff }}” 的消息将提醒给 -->
      <template v-if="remindInfo.type == 2">
        客户 <span style="color:#ff8933"> {{remindInfo.follow_staff}} </span> 删除员工<span style="color:#ff8933"> {{  remindInfo.name }} </span> 的消息将提醒给
      </template>
      <template v-else>
        员工 <span style="color:#ff8933"> {{  remindInfo.name }} </span> 删除客户<span style="color:#ff8933"> {{remindInfo.follow_staff}} </span> 的消息将提醒给
      </template>
      <span
        v-show="remindInfo.manager.length > 0"
        v-for="(i, index) in remindInfo.manager"
        :key="i.id"
        style="color:#ff8933"
      >{{ i.work_user.name }}
        <span v-show="remindInfo.manager.length - 1 != index">、</span></span>
      <span
        v-show="remindInfo.manager.length <= 0"
        style="color:#ff8933"
      >--</span>

    </span>
    <template #footer="scope">
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click="send(scope.row)"
        >确定发送</el-button>
      </span>
    </template>
  </el-dialog>
  <ZWPagination
    @submit="page"
    ref="PaginationRef"
  ></ZWPagination>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { Picture as IconPicture } from '@element-plus/icons-vue'
import { ElMessage, ElTable } from 'element-plus'
import { getDelList, remindStaff } from '@/api/employe/staffsupervision/deletrecord'
import router from '@/router'
import ZWPagination from '@/components/ZWPagination.vue'
import { useStore } from '@/store'
import bus from '@/utils/bus'
import { getSetionId, tableH, reSize } from '@/utils/utils'

const store = useStore()

const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const dialogVisible = ref(false)
const tabH = ref('500px')
const emit = defineEmits(['select', 'getAlertUser'])
const params = ref({
  page: 1,
  page_size: 15,
  work_user_ids: '',
  keyword: '',
  del_cust_time: []
})
const allotId = ref('')
const customer = ref([]) // 选中的删除客户
const remindInfo = ref({
  type: 1,
  name: '',
  follow_staff: '',
  manager: ''
})
const handleSelectionChange = (val: any) => {
  let arr = []
  val.map((item: any) => {
    arr.push(item.id)
  })
  customer.value =  Array.from(new Set(arr))
  emit('select', customer.value)
}
const tableData = ref([])
// 分配员工
const allot = (val: any) => {
  // bus.emit('clean2', member.value.selectedMember)
  bus.emit('allot')
  const arr = []
  arr.push(val.id)
  customer.value = arr
}
// 详情
const detail = (row: any) => {
  router.push(`/customer/manage/list/detail?id=${row.cust_id}`)
}
const getAlertUser2 = (data?: any) => {
  remindInfo.value.manager = data
  return data
}
// 一键提醒
const remind = (row: any) => {
  emit('getAlertUser', getAlertUser2)
  allotId.value = row.id
  dialogVisible.value = true
  remindInfo.value.type = row.type
  remindInfo.value.name = row.work_user_name
  remindInfo.value.follow_staff = row.cust_name
  // remindInfo.value.manager = getAlertUser2()
}
const send = (value: any) => {
  const params = {
    trace_id: allotId.value
  }
  remindStaff(params)
    .then((res: any) => {
      dialogVisible.value = false
      ElMessage.success('发送消息成功')
    })
    .catch((e: any) => {
      dialogVisible.value = false
      console.log(e)
    })
}
// 获取删除列表数据
const getDelete = (val?: any) => {
  params.value.work_user_ids = val.work_user_list
  params.value.keyword = val.keyword
  params.value.del_cust_time = val.del_cust_time
  params.value.page = val.page ? val.page : 1
  params.value.page_size = val.page ? val.page_size : 15
  getDelList(params.value).then((data: any) => {
    tableData.value = data.data
    store.dispatch('pageTotal', data.total)
  })
}
// 分页信息
const page = (data: any) => {
  params.value.page = data.page
  params.value.page_size = data.page_size
  getDelete(params.value)
}
// 表格的自适应高度
const getTableH = () => {
  const h = parseInt(tableH('.table_box'))
  tabH.value = h - 55 + 'px'
}
onMounted(()=>{
  reSize(getTableH)
  getTableH()
})
getDelete(params)
defineExpose({
  getDelete,
  customer,
  params,
  getTableH
})
</script>
<style scoped lang="scss">
.tableData {
  background: #ffffff;
  box-sizing: border-box;
  border: 1px solid #eaeaea;

  .cusInfo {
    display: flex;
    align-items: center;

    .avatar {
      width: 36px;
      height: 36px;
      border-radius: 4px;

      .image-slot {
        .el-icon {
          font-size: 36px;
        }
      }
    }

    .name {
      // width: 80px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      margin: 0 5px;
    }

    .channel {
      font-size: 14px;
      color: #60b900;
      margin-right: 3px;
    }

    .sex {
      width: 18px;
      height: 18px;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .handle {
    display: flex;
    align-items: center;

    .btn_item {
      width: 80px;
      height: 24px;
      text-align: center;
      line-height: 24px;
      background: #ffffff;
      border-radius: 2px;
      border: 1px solid #eaeaea;
      cursor: pointer;

      &:nth-child(2) {
        margin: 0 12px;
      }
    }
  }

  .tagList {
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    .tag_item {
      padding: 4px;
      color: #ff6b00;
      margin: 0 10px 10px 0;
      border-radius: 2px;
      border: 1px solid #ffc69e;

      &:nth-child(n + 6) {
        display: none;
      }
    }
  }

  .emty {
    margin-top: 200px;
    // display: flex;
    // flex-direction: column;
  }

  .emty_img {
    transform: translateX(15%);
  }

  .emty_title {
    margin-top: -50px;
  }
}

.conceal {
  display: flex;
  flex-wrap: wrap;

  &_tag_item {
    padding: 4px;
    color: #ff6b00;
    margin: 0 10px 10px 0;
    border-radius: 2px;
    border: 1px solid #ffc69e;
  }
}

.tag_box {
  display: flex;
  display: -webkit-box; //谷歌
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2; //显示几行
  overflow: hidden;

  .form-tags {
    margin: 0px 10px 0px 0px;
  }
}
</style>
